package com.zysdev.opengldouyin1demo.filter.addFilter

import android.content.Context
import android.opengl.GLES20
import com.zysdev.opengldouyin1demo.R
import com.zysdev.opengldouyin1demo.filter.BaseFBOFilter

class SoulFilter : BaseFBOFilter {
    private var scalePercent = 0
    private var scale = 0.0f //缩放，越大就放的越大，到2倍终止

    private var mixturePercent = 0
    private var mix = 0.0f //透明度，越大越透明，到0结束

    //出窍速度，越大则越快
    private val speed=0.05f

    constructor(context: Context) : super(
        context,
        R.raw.base_vert,
        R.raw.soul_frag
    ) {
        //得到变量句柄
        scalePercent = GLES20.glGetUniformLocation(program, "scalePercent")
        mixturePercent = GLES20.glGetUniformLocation(program, "mixturePercent")

    }

    override fun beforeDraw() {
        super.beforeDraw()
        //使用变量句柄，给变量复制
        GLES20.glUniform1f(scalePercent, scale + 1.0f)

        //        1  -  0     1减到 0
        GLES20.glUniform1f(mixturePercent, 1.0f - mix)


        scale += speed
        mix += speed
        if (scale >= 1.0) {
            scale = 0.0f
        }
        if (mix >= 1.0) {
            mix = 0.0f
        }

    }
}